package com.example.demo.dao;

import com.example.demo.entity.Student;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.Modifying;
import org.springframework.data.jpa.repository.Query;

import javax.transaction.Transactional;
import java.util.List;

/**
 * JpaRepository<Student,Integer> 中
 * Student是对应的要操作的学生实体类
 * Integer是对应的实体类ID主键类型
 */
public interface StuJpaDao extends JpaRepository<Student,Integer> {


    //jpa自带查询sql语句
    @Query("select c from stu c where c.id=?1")
    Student getOnes(int id);
    //使用原生数据库sql语句查询方式
    @Query(value = "select * from stu where id=?1",nativeQuery = true)
    Student getOneByNativeQuery(int id);
    //使用原生数据库sql语句更新数据方式
    @Transactional
    @Modifying
    @Query(value = "update stu set name=?2  where id=?1",nativeQuery = true)
    int  updateStu(int id,String newName);
    //查询name不为空的学生列表   select * from stu where name is not null
    List<Student>  findByNameNotNull();
    //查询name字段包含name这个字符串的所有学生 where name like "%name%"
    // select * from stu where name like "%峰%"
    List<Student>  findByNameContaining(String name );
    // select * from stu where name like "%峰%" and age = age
    List<Student> findByNameContainingAndAge(String name,int age);

    // select * from stu where name like "%峰%" and age in (age1,age2)
    List<Student> findByNameContainingAndAgeBetween(String name,int age1,int age2);
    // select * from stu where name = OR name= order by age desc  从大的往小排
    List<Student> findByNameOrNameOrderByAgeDesc(String name,String name2);
}
